// ScaleShape.java
// by Alexandre Martins, 2013
// MAC 5857 - Prof. Leonidas

package example.ilm.model;

import ilm.framework.assignment.model.DomainAction;
import ilm.framework.domain.DomainModel;

public class ScaleShape extends DomainAction {

    private IlmDomainModel _domain;
    private Shape shape;
    private double scale;
    private static int cnt = 0;

    public ScaleShape() {
        super("scaleShape[" + (++cnt) + "]", "scaled a shape");
        this.scale = 1.0;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    public void scale(double s) {
        this.scale = s;
        if(this.scale < 1e-5) // scale can't be negative (??)
            this.scale = 1e-5;
    }

    /**
     * Defines the DomainModel on which this action can happen. It must be
     * converted to IlmDomainModel so this class can call its specific methods.
     * 
     * @param an
     *            IlmDomainModel
     */
    public void setDomainModel(DomainModel model) {
        _domain = (IlmDomainModel) model;
    }

    /**
     * Method that is called when this action is executed.
     */
    protected void executeAction() {
        _domain.scaleShape(_currentState, shape, scale);
    }

    /**
     * Method that is called when this action is undone.
     */
    protected void undoAction() {
        _domain.scaleShape(_currentState, shape, 1.0 / scale);
    }

    /**
     * Compares this to another DomainAction.
     */
    public boolean equals(DomainAction a) {
        ScaleShape b = (ScaleShape)a;
        return (getName().equals(a.getName()) && getDescription().equals(a.getDescription()) && shape.equals(b.shape) && Math.abs(scale - b.scale) < 1e-5);
    }

    /**
     * This method is used for converting this object into string
     * 
     * @see example.ilm.model.IlmDomainConverter
     */
    public String toString() {
        return "" + 
            "<scaleshape>" + 
                "<name>" + getName() + "</name>" + 
                "<description>" + getDescription() + "</description>" + 
                "<scale>" + scale + "</scale>" + 
                "<shape>" + 
                    shape.toString() + 
                "</shape>" + 
            "</scaleshape>";
    }
}
